// pages/customer/customer.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    tabList:[
      {
        img:'../../images/bmgh.png',
        acimg:'../../images/bmgh(1).png',
        name:'部门公海'
      },
      { 
        img:'../../images/gsgh.png',
        acimg:'../../images/gsgh(1).png',
        name:'公司公海'
      },
      { 
        img:'../../images/yxkh.png',
        acimg:'../../images/yxkh(1).png',
        name:'意向客户'
      },
      { 
        img:'../../images/hlxs.png',
        acimg:'../../images/hlxs(1).png',
        name:'个人私海'
      },
      {
        img:'../../images/tjbb.png',
        acimg:'../../images/tjbb(1).png',
        name:'统计报表'
      },
    ],
    //公司公海搜索框绑定值
    bmghValue:'',
    gsghValue:'',
    yxkhValue:'',
    hlxsValue:'',
    //部门公海
    bmghTab: [],
    //公司公海数据
    gsghTab:[],
    //意向客户
    yxkhTab:[],
    //海量线索
    hlxsTab:[],
    color:'#ffffff',
    currentTab:0
  },
  call(e){
    // console.log("进入拨打方法")
    // console.log(e.currentTarget.dataset.id)
    // wx.makePhoneCall({
    //   phoneNumber: e.currentTarget.dataset.id.activePhone,
    //   complete(){
    //   }
    // })
      var appid = wx.getStorageSync('appid')
      var activePhone= wx.getStorageSync('username')
      var token = wx.getStorageSync('TOKEN')
      wx.request({
        url: 'http://123.60.30.116:9999/app/call/app_bind',
        method:"POST",
        data: {
          passivePhone:e.currentTarget.dataset.id.activePhone,
          activePhone:activePhone,
          appid:appid,
        },
        header: {
          'content-type':'application/json', // 默认值
          'X-Access-Token':token
        },
        success (res) {
          wx.showToast({ 
            title: res.data.message, 
            icon: 'success', 
            duration: 2000 
            })
            console.log(res.data.data)
            if(res.data.data.data != ''){
              wx.makePhoneCall({
                phoneNumber: res.data.data,
              })
            }

            let str = JSON.stringify(e.currentTarget.dataset.id.id)
            wx.navigateTo({
              url: '/pages/set/set?jsonStr='+str,
            })
        },
        fail(){
          wx.showToast({ 
            title: '接口调用错误！', 
            icon: 'fail', 
            duration: 1000 
            })
        }
      })
  },
  //点击顶部
  clickImg(event){
    var $this = this;
    this.setData({
      currentTab:event.currentTarget.dataset.index
    })
    if(event.currentTarget.dataset.index == 0){
      console.log("部门公海页面")
      this.getbmgh()
    }else if(event.currentTarget.dataset.index == 1){
      console.log("公司公海页面！")
      this.getgsgh()
    }else if(event.currentTarget.dataset.index == 2){
    }else if(event.currentTarget.dataset.index == 3){
      console.log("海量线索页面！")
      this.gethlxs()
    }else if(event.currentTarget.dataset.index == 4){
      console.log("统计报表页面！")
      var $this = this
      wx.navigateTo({
        url: '/pages/form/form',
      })
    }
  },
  //部门工海
  onbmghSearch(event){   
    var $this = this
    if(event.detail == ''){
      this.getbmgh()
    }else{
    $this.setData({
      bmghValue:event.detail
    })
    var $this = this
    var token = wx.getStorageSync('TOKEN')
    var parentId = wx.getStorageSync('parentId')
    wx.request({
      url: 'http://123.60.30.116:9999/app/phone/list', //仅为示例，并非真实的接口地址
      method:"POST",
      data: {
        departId:0,
        activePhone:$this.data.bmghValue
      },
      header: {
        'content-type': 'application/json', // 默认值
        'X-Access-Token':token
      },
      success (res) {
        console.log(res.data.data)
        $this.setData({
          bmghTab:res.data.data
        })
      },
      fail(){
        wx.showToast({ 
          title: '接口调用错误！', 
          icon: 'fail', 
          duration: 1000 
          })
      }
    })
  }
},
  onbmghCancel(){
    var $this = this
    $this.setData({
      gsghValue:''
    })
  },
  //公司公海
  ongsghSearch(event){
    var $this = this
    if(event.detail == ''){
      this.getgsgh()
    }else{
    $this.setData({
      gsghValue:event.detail
    })
    var $this = this
    var token = wx.getStorageSync('TOKEN')
    wx.request({
      url: 'http://123.60.30.116:9999/app/phone/list', //仅为示例，并非真实的接口地址
      method:"POST",
      data: {
        roleId:0,
        activePhone:$this.data.gsghValue
      },
      header: {
        'content-type': 'application/json', // 默认值
        'X-Access-Token':token
      },
      success (res) {
        $this.setData({
          gsghTab:res.data.data
        })
      },
      fail(){
        wx.showToast({ 
          title: '接口调用错误！', 
          icon: 'fail', 
          duration: 1000 
          })
      }
    })
  }
  },
  ongsghCancel(){
    var $this = this
    $this.setData({
      gsghValue:''
    })
  },
  //意向客户
onyxkhSearch(event){
  var $this = this
    if(event.detail == ''){
      this.getyxkh()
    }else{
    $this.setData({
      yxkhValue:event.detail
    })
    var $this = this
    var token = wx.getStorageSync('TOKEN')
    wx.request({
      url: 'http://123.60.30.116:9999/app/phone/list', //仅为示例，并非真实的接口地址
      method:"POST",
      data: {
        status:'1',
        activePhone:$this.data.yxkhValue
      },
      header: {
        'content-type': 'application/json', // 默认值
        'X-Access-Token':token
      },
      success (res) {
        $this.setData({
          yxkhTab:res.data.data
        })
      },
      fail(){
        wx.showToast({ 
          title: '接口调用错误！', 
          icon: 'fail', 
          duration: 1000 
          })
      }
    })
  }
},
onyxkhCancel(){
  var $this = this
  $this.setData({
    yxkhValue:''
  })
},
  jumpAdd(){
    wx.navigateTo({
      url: '/pages/add/add',
    })
  },
  //海量线索
  onhlxsSearch(event){
    if(event.detail == ''){
      this.gethlxs()
    }else{
    $this.setData({
      hlxsValue:event.detail
    })
    var $this = this
    var token = wx.getStorageSync('TOKEN')
    wx.request({
      url: 'http://123.60.30.116:9999/app/phone/list', //仅为示例，并非真实的接口地址
      method:"POST",
      data: {
        activePhone:$this.data.hlxsValue
      },
      header: {
        'content-type': 'application/json', // 默认值
        'X-Access-Token':token
      },
      success (res) {
        res.data.data.forEach(item=>{
          if(item.status == 0){
            item.statusName = '一般客户'
            item.color = '#4575E2'
          }else if(item.status == 1){
            item.statusName = '意向客户'
            item.color = '#40D084'
          }else if(item.status == 2){
            item.statusName = '无意向客户'
            item.color = '#6797C0'
          }else if(item.status == 3){
            item.statusName = '成交客户'
            item.color = '#19149'
          }else if(item.status == 4){
            item.statusName = '黑名单'
            item.color = '#565656'
          }
        })
        $this.setData({
          hlxsTab:res.data.data
        })
      },
      fail(){
        wx.showToast({ 
          title: '接口调用错误！', 
          icon: 'loading', 
          duration: 1000 
          })
      }
    })
  }
  },
  onhlxsCancel(){
    var $this = this
    $this.setData({
      hlxsValue:''
    })
  },
  jumpData(e){
    console.log('我要传的值',e)
    var id = e.currentTarget.dataset.id
    console.log('我传入的data-id',id)
    let str = JSON.stringify(id)
    wx.navigateTo({
      url: '/pages/set/set?jsonStr='+str,
    })
  },
  getbmgh(){
    var $this = this
    var token = wx.getStorageSync('TOKEN')
    wx.request({
      url: 'http://123.60.30.116:9999/app/phone/list', //仅为示例，并非真实的接口地址
      method:"POST",
      data: {
        departId:0
      },
      header: {
        'content-type': 'application/json', // 默认值
        'X-Access-Token':token
      },
      success (res) {
        res.data.data.forEach(item=>{
          let str = item.activePhone
          item.phone = str.substr(0,3)+'****'+str.substr(7)
        })
        $this.setData({
          bmghTab:res.data.data
        })
        console.log($this.data.bmghTab)
      },
      fail(){
        wx.showToast({ 
          title: '接口调用错误！', 
          icon: 'fail', 
          duration: 1000 
          })
      }
    })
  },
  getgsgh(){
    var $this = this
    var token = wx.getStorageSync('TOKEN')
      wx.request({
        url: 'http://123.60.30.116:9999/app/phone/list', //仅为示例，并非真实的接口地址
        method:"POST",
        data: {
          roleId:0
        },
        header: {
          'content-type': 'application/json', // 默认值
          'X-Access-Token':token
        },
        success (res) {
          res.data.data.forEach(item=>{
            let str = item.activePhone
            item.phone = str.substr(0,3)+'****'+str.substr(7)
            
            if(item.status == 0){
              item.statusName = '一般客户'
              item.color = '#F19149'
            }else if(item.status == 1){
              item.statusName = '意向客户'
              item.color = '#FF2D3C'
            }else if(item.status == 2){
              item.statusName = '无意向客户'
              item.color = '#6797C0'
            }else if(item.status == 3){
              item.statusName = '成交客户'
              item.color = '#40D084'
            }else if(item.status == 4){
              item.statusName = '黑名单'
              item.color = '#565656'
            }
          })

          $this.setData({
            gsghTab:res.data.data
          })
        },
        fail(){
          wx.showToast({ 
            title: '接口调用错误！', 
            icon: 'fail', 
            duration: 1000 
            })
        }
      })
  },
  
  gethlxs(){
    var $this = this
    var token = wx.getStorageSync('TOKEN')
    var id = wx.getStorageSync('id')
      wx.request({
        url: 'http://123.60.30.116:9999/app/phone/list', 
        method:"POST",
        data: {
         userId:id
        },
        header: {
          'content-type': 'application/json', // 默认值
          'X-Access-Token':token
        },
        success (res) {
          res.data.data.forEach(item=>{
              let str = item.activePhone
              item.phone = str.substr(0,3)+'****'+str.substr(7)
            if(item.status == 0){
              item.statusName = '一般客户'
              item.color = '#4575E2'
            }else if(item.status == 1){
              item.statusName = '意向客户'
              item.color = '#40D084'
            }else if(item.status == 2){
              item.statusName = '无意向客户'
              item.color = '#6797C0'
            }else if(item.status == 3){
              item.statusName = '成交客户'
              item.color = '#F19149'
            }else if(item.status == 4){
              item.statusName = '黑名单'
              item.color = '#565656'
            }
          })
          $this.setData({
            hlxsTab:res.data.data
          })
          console.log($this.data.hlxsTab)
        },
        fail(){
          wx.showToast({ 
            title: '接口调用错误！', 
            icon: 'fail', 
            duration: 1000 
            })
        }
      })
  },

  //意向客户页面点击
  ybkh(){
    wx.navigateTo({
      url: '/pages/ybkh/ybkh',
    })
  },
  yxkh(){
    wx.navigateTo({
      url: '/pages/yxkh/yxkh',
    })
  },
  wyxkh(){
    wx.navigateTo({
      url: '/pages/wyxkh/wyxkh',
    })
  },
  cjkh(){
    wx.navigateTo({
      url: '/pages/cjkh/cjkh',
    })
  },
  hmd(){
    wx.navigateTo({
      url: '/pages/hmd/hmd',
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var $this = this
    $this.setData({
      currentTab:0
    }),
    this.getbmgh(),
    this.getgsgh(),
    this.gethlxs()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  },
})